home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume6 / yaccref < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  12.5 KB

  1. Subject: v06i051:  Cross-reference for Yacc (yyref)
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.UUCP
  4.  
  5. Submitted by: Cathy Taylor <seismo!mcvax!comp.lancs.ac.uk!cathy>
  6. Mod.sources: Volume 6, Issue 51
  7. Archive-name: yyref
  8.  
  9. [  I changed the name to yyref, added a Makefile, and broke up the
  10.    mail message I received into a README and a manpage.  --r$ ]
  11.  
  12. The following source forms the xref program for syntactically correct
  13. Yacc files.
  14.  
  15. All tokens and non-terminals in the grammar are located, together with
  16. a list of the line numbers at which they occur. The output is a
  17. numbered listing of the Yacc grammar up to the second '%%' or EOF,
  18. followed by the xref table. Should a syntax error occur before normal
  19. termination, the grammar listing ends and as much of the table as has
  20. been constructed is printed. All comments /* .. */ , %{ .. %} and
  21. actions { .. } are ignored, although they appear in the listing.
  22.  
  23. #!/bin/sh
  24. # This is a shell archive.  Remove anything before this line,
  25. # then unpack it by saving it in a file and typing "sh file".
  26. # Contents:  Makefile README yyref.1 yyref-grammer yyref-lex
  27. #    yyref-line.h
  28.  
  29. echo x - Makefile
  30. sed 's/^XX//' > "Makefile" <<'@//E*O*F Makefile//'
  31.  
  32. XXCFLAGS=
  33. XXyyref:    yyref-lex yyref-grammar
  34. XX    lex yyref-lex
  35. XX    yacc -vd yyref-grammar
  36. XX    cc $(CFLAGS) y.tab.c -o yyref
  37.  
  38. XXclean:
  39. XX    rm -f lex.yy.c y.tab.? y.output yyref
  40. @//E*O*F Makefile//
  41. chmod u=rw,g=rw,o=rw Makefile
  42.  
  43. echo x - README
  44. sed 's/^XX//' > "README" <<'@//E*O*F README//'
  45. XXA COUPLE OF NOTES ON CONFIGURATION PARAMETERS:
  46. XX(See the code section of yyref-grammar)
  47. XX    Table data is stored in "table[]", which holds MAXIDENTS terminal/non-
  48. XXterminal names, each of length <= MAXCHARS. A total of MAXDEFS defining (rhs of
  49. XXrule) occurances can be held, and a total of MAXOCCS lhs occurances. Each
  50. XXidentifier which may be the start token is printed following the message held
  51. XXin "start_maybe[]" - likewise all possible tokens are accompanied by
  52. XX"token_maybe[]". All defining occurances are printed following
  53. XX"declared_at_mark[]", and all other occurances following "occurs_at_mark[]".
  54.  
  55. @//E*O*F README//
  56. chmod u=rw,g=rw,o=rw README
  57.  
  58. echo x - yyref.1
  59. sed 's/^XX//' > "yyref.1" <<'@//E*O*F yyref.1//'
  60. XX.TH YYREF 1 LOCAL
  61. XX.SH NAME
  62. XXyyref \- generate cross\-reference for YACC input
  63. XX.SH SYNOPSIS
  64. XX.B yyref
  65. XX[ < inputfile ]
  66. XX.SH DESCRIPTION
  67. XX.I Yyref
  68. XXgenerates cross\-references for
  69. XX.IR yacc (1)
  70. XXinput files.
  71. XXIt reads source from standard input, and upon EOF or seeing the second
  72. XX.B %%
  73. XXthat marks the end of the rules portion, generates a cross\-reference
  74. XXlist of all parser tokens.
  75. XX.PP
  76. XXIn case of syntax errors in the input,
  77. XX.I yyref
  78. XXtries to generate as much information as it can.
  79. XX.PP
  80. XXThe output consists of a number listing of the header and rules part,
  81. XXfollowed by the cross\-reference:
  82. XX.RS
  83. XX.nf
  84. XX   1 :    %{
  85. XX   2 :    # include <ctype.h>
  86. XX   3 :    %}
  87. XX   4 :    
  88. XX   5 :    
  89. XX   6 :        /*******************************************************\
  90. XX   7 :        *                            *
  91. XX   8 :        *    Date : Fri Jul  4 00:50:04 BST 1986        *
  92. XX   9 :        *                            *
  93. XX  10 :        \*******************************************************/
  94. XX  11 :    
  95. XX  12 :    %token    IDENTIFIER START_TOKEN NUMBER
  96. XX  13 :    
  97. XX  14 :    %start    small
  98. XX  15 :    
  99. XX  16 :    %%
  100. XX  17 :    
  101. XX  18 :    small
  102. XX  19 :        :    start middle
  103. XX  20 :                {
  104. XX  21 :                    printf("\n\nMiddle");
  105. XX  22 :                    yyclearin;
  106. XX  23 :                    return(0);
  107. XX  24 :                }
  108. XX  25 :            end postamble
  109. XX  26 :        ;
  110. XX  27 :    
  111. XX  28 :    start
  112. XX  29 :        :    /* empty */
  113. XX  30 :        |    START_TOKEN
  114. XX  31 :        ;
  115. XX  32 :    
  116. XX  33 :    middle
  117. XX  34 :        :
  118. XX  35 :        ;
  119. XX  36 :    
  120. XX  37 :    middle
  121. XX  38 :        :    MID_TOKEN
  122. XX  39 :        |    /* empty */
  123. XX  40 :        ;
  124. XX  41 :    
  125. XX  42 :    %%
  126.  
  127.  
  128. XX'small' -
  129. XX~~~~~~~        *18 , never occurs on rhs of rule - start rule?
  130. XX'start' -
  131. XX~~~~~~~        *28 , 19
  132. XX'middle' -
  133. XX~~~~~~~        *33 , *37, 19
  134. XX'end' -
  135. XX~~~~~~~        is not declared - token??, 25
  136. XX'postamble' -
  137. XX~~~~~~~        is not declared - token??, 25
  138. XX'START_TOKEN' -
  139. XX~~~~~~~        is not declared - token??, 30
  140. XX'MID_TOKEN' -
  141. XX~~~~~~~        is not declared - token??, 38
  142.  
  143. XX    End of X-ref
  144. XX    ~~~~~~~~~~~~
  145. XX.fi
  146. XX.RE
  147. XX.SH BUGS
  148. XXShould be able to understand the
  149. XX.I %token
  150. XXand similar directives.
  151. @//E*O*F yyref.1//
  152. chmod u=rw,g=rw,o=rw yyref.1
  153.  
  154. echo x - yyref-grammer
  155. sed 's/^XX//' > "yyref-grammer" <<'@//E*O*F yyref-grammer//'
  156. XX%{
  157.  
  158. XX# include <ctype.h>
  159. XX# include <stdio.h>
  160.  
  161. XX%}
  162.  
  163.  
  164. XX    /*******************************************************\
  165. XX    *                            *
  166. XX    *    X_reference program for YACC files        *
  167. XX    *    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        *
  168. XX    *                            *
  169. XX    *    Cathy Taylor,                    *
  170. XX    *    c/o Department of Computing,            *
  171. XX    *    University of Lancaster,            *
  172. XX    *    Bailrigg, Lancaster, England.            *
  173. XX    *    Date : Fri Jul  4 00:50:04 BST 1986        *
  174. XX    *                            *
  175. XX    \*******************************************************/
  176.  
  177.  
  178. XX    /***********************************************\
  179. XX    *                        *
  180. XX    *    Yacc Input Syntax            *
  181. XX    *    ~~~~~~~~~~~~~~~~~            *
  182. XX    *                        *
  183. XX    *    Adapted from the document        *
  184. XX    *    'YACC - Yet Another Compiler Compiler'    *
  185. XX    *        by                *
  186. XX    *       S. C. Johnson            *
  187. XX    *                        *
  188. XX    *    Date: Tue Jul  1 02:40:18 BST 1986    *
  189. XX    *                        *
  190. XX    \***********************************************/
  191.  
  192.  
  193. XX%token    IDENTIFIER CHARACTER NUMBER
  194. XX%token    LEFT RIGHT NONASSOC TOKEN PREC TYPE START UNION
  195. XX%token    PER PERCURL ACT
  196. XX%token    COLON SEMICOLON COMMA OR LESS GREATER
  197.  
  198. XX%start    spec
  199.  
  200. XX%%
  201.  
  202. XXspec
  203. XX    :    defs PER rules tail
  204. XX            {
  205. XX                printf("\n\n");
  206. XX                yyclearin;
  207. XX                return(0);
  208. XX            }
  209. XX    ;
  210.  
  211. XXtail
  212. XX    :    /* empty */
  213. XX    |    PER 
  214. XX    ;
  215.  
  216. XXdefs
  217. XX    :    /* empty */
  218. XX    |    def_bk
  219. XX    ;
  220.  
  221. XXdef_bk
  222. XX    :    def
  223. XX    |    def_bk def
  224. XX    ;
  225.  
  226. XXdef
  227. XX    :    START IDENTIFIER
  228. XX    |    UNION
  229. XX    |    PERCURL
  230. XX    |    rword tag nlist
  231. XX    ;
  232.  
  233. XXrword
  234. XX    :    TOKEN
  235. XX    |    LEFT
  236. XX    |    RIGHT
  237. XX    |    NONASSOC
  238. XX    |    TYPE
  239. XX    ;
  240.  
  241. XXtag
  242. XX    :    /* empty */
  243. XX    |    LESS IDENTIFIER GREATER
  244. XX    ;
  245.  
  246. XXnlist
  247. XX    :    nmno
  248. XX    |    nlist opt_comma nmno
  249. XX    ;
  250.  
  251. XXopt_comma
  252. XX    :    /* empty */
  253. XX    |    COMMA
  254. XX    ;
  255.  
  256. XXnmno
  257. XX    :    IDENTIFIER opt_num
  258. XX    ;
  259.  
  260. XXopt_num
  261. XX    :    /* empty */
  262. XX    |    NUMBER
  263. XX    ;
  264. XXrules
  265. XX    :    rule
  266. XX    |    rules rule
  267. XX    ;
  268.  
  269. XXrule
  270. XX    :    IDENTIFIER
  271. XX        {
  272. XX            yyaction(ON_C_IDENT,line);
  273. XX        }
  274. XX        COLON body SEMICOLON
  275. XX    ;
  276.  
  277. XXbody
  278. XX    :    body_block
  279. XX    |    body OR body_block
  280. XX    ;
  281.  
  282. XXbody_block
  283. XX    :    /* empty */
  284. XX    |    body_block body_entity
  285. XX    ;
  286.  
  287. XXbody_entity
  288. XX    :    opt_prec id_ent
  289. XX    |    ACT
  290. XX    ;
  291.  
  292. XXid_ent
  293. XX    :    IDENTIFIER
  294. XX        {
  295. XX            yyaction(ON_IDENT,line);
  296. XX        }
  297. XX    |    CHARACTER
  298. XX    ;
  299.  
  300. XXopt_prec
  301. XX    :    /* empty */
  302. XX    |    PREC
  303. XX    ;
  304.  
  305.  
  306. XX%%
  307.  
  308. XX# include    <stdio.h>
  309. XX# include    "lex.yy.c"
  310. XX# include    "yyref-line.h"
  311.  
  312. XX#define    ON_C_IDENT    000
  313. XX#define    ON_IDENT    001
  314.  
  315. XX#define    MAXIDENTS    1000
  316. XX#define    MAXCHARS    100
  317. XX#define    MAXDEFS        20
  318. XX#define    MAXOCCS        1000
  319.  
  320. XXstruct    IREC {
  321. XX        char    ident[MAXCHARS];
  322. XX        int    desc[MAXDEFS];
  323. XX        int    nextdesc;
  324. XX        int    occ[MAXOCCS];
  325. XX        int    nextocc;
  326. XX        } table[MAXIDENTS];
  327.  
  328.  
  329. XXyyaction (action,ln)
  330. XXint    action;
  331. XXint    ln;
  332. XX{
  333. XX    int    id;
  334. XX    
  335. XX    id = 0;
  336. XX    while (    strcmp(table[id].ident,yytext) != 0 && strcmp(table[id].ident,"") != 0 )
  337. XX        id++;
  338.  
  339. XX    if ( strcmp(table[id].ident, yytext) != 0 )
  340. XX    {
  341.  
  342. XX    /*******************************************************\
  343. XX    *                            *
  344. XX    *    New non-terminal to be stored.            *
  345. XX    *    No distinction is made here between tokens    *
  346. XX    *    and (non) terminals.                *
  347. XX    *                            *
  348. XX    \*******************************************************/
  349.  
  350. XX        strcpy(table[id].ident,yytext);
  351. XX        table[id].nextdesc = 0;
  352. XX        table[id].nextocc = 0;
  353. XX    } /* fi */
  354.  
  355. XX    switch (action) {
  356. XX    case ON_C_IDENT:
  357.  
  358. XX    /*******************************************************\
  359. XX    *                            *
  360. XX    *    Add to list of definition lines.        *
  361. XX    *                            *
  362. XX    \*******************************************************/
  363.  
  364. XX        table[id].desc[table[id].nextdesc++] = ln;
  365. XX        break;
  366.  
  367. XX    case ON_IDENT:
  368. XX                
  369. XX    /*******************************************************\
  370. XX    *                            *
  371. XX    *    Add to list of occurance lines.            *
  372. XX    *                            *
  373. XX    \*******************************************************/
  374.  
  375. XX        table[id].occ[table[id].nextocc++] = ln;
  376. XX        break;
  377.  
  378. XX    default        :
  379. XX        fprintf (stdout, "yyaction: invalid action\n");
  380. XX        return (-1);
  381. XX        } /* hctiws */
  382. XX    return (0);
  383. XX} /* corp */
  384.  
  385. XXnline(ln)
  386. XXint    ln;
  387. XX{
  388. XX    printf("%4d :\t",ln);
  389. XX}
  390.  
  391.  
  392. XX    char    declared_at_mark[] = "*";
  393. XX    char    occurs_at_mark[] = "";
  394. XX    char    token_maybe[] = "is not declared - token??";
  395. XX    char    start_maybe[] = "never occurs on rhs of rule - start rule?";
  396. XX    
  397. XX/*
  398. XX*    Strings for output
  399. XX*/
  400.  
  401. XXmain ()
  402. XX{
  403. XX    int    ind,id;
  404.  
  405. XX    strcpy(table[0].ident,"");
  406.  
  407. XX    line = 0;
  408. XX    nline(++line);
  409.  
  410. XX    yyparse ();
  411.  
  412. XX    id = 0;
  413. XX    while( strcmp(table[id].ident,"") != 0 )
  414. XX    {
  415. XX        printf("\n'%s' -\n~~~~~~~\t\t",table[id].ident);
  416. XX        if (table[id].nextdesc == 0 )
  417. XX            printf("%s",token_maybe);
  418. XX        else
  419. XX        {
  420. XX            ind = 0;
  421. XX            printf("*%d ",table[id].desc[ind++]);
  422. XX            for ( ind=1; ind < table[id].nextdesc ; ind++)
  423. XX            printf(", %s%d",declared_at_mark,table[id].desc[ind]);
  424. XX        }
  425. XX        if (table[id].occ[0] == 0)
  426. XX            printf(", %s",start_maybe);
  427. XX        else
  428. XX        {
  429. XX            for ( ind = 0; ind < table[id].nextocc ; ind++ )
  430. XX            printf(", %s%d",occurs_at_mark,table[id].occ[ind]);
  431. XX        }
  432. XX        id++;
  433. XX    }
  434. XX    printf("\n\n\tEnd of X-ref\n\t~~~~~~~~~~~~\n");
  435. XX} /* niam */
  436.  
  437. XXyyerror(mess)
  438. XXchar    *mess;
  439. XX{
  440. XX    printf("\n\t%s\n",mess);
  441. XX} /* corp */
  442. @//E*O*F yyref-grammer//
  443. chmod u=rw,g=rw,o=rw yyref-grammer
  444.  
  445. echo x - yyref-lex
  446. sed 's/^XX//' > "yyref-lex" <<'@//E*O*F yyref-lex//'
  447.  
  448. XX    /*******************************************************\
  449. XX    *                            *
  450. XX    *    X_ref for YACC - LEX file            *
  451. XX    *    ~~~~~~~~~~~~~~~~~~~~~~~~~            *
  452. XX    *    Date: Tue Jul  1 03:36:21 BST 1986        *
  453. XX    *                            *
  454. XX    \*******************************************************/
  455.  
  456. XX%{
  457.  
  458. XX# include    <stdio.h>
  459. XX# include    "yyref-line.h"
  460.  
  461. XX#define        TRUE 1
  462. XX#define         FALSE 0
  463.  
  464. XXint    recognised;
  465. XXchar    c;
  466.  
  467. XX%}
  468.  
  469. XX    /* abbreviations */
  470.  
  471. XXdigit        [0-9]
  472. XXu_case        [A-Z]
  473. XXl_case        [a-z]
  474. XXid_char        [A-Za-z0-9_]
  475. XXletter        [A-Za-z]
  476. XXwhite        [\t ]
  477.  
  478.  
  479. XX%%
  480.  
  481. XX"/*"            {
  482. XX                ECHO;
  483. XX                recognised = FALSE;
  484. XX                c = nextchar();
  485. XX                while (recognised == FALSE)
  486. XX                {
  487. XX                    while (c != '*')
  488. XX                        c = nextchar();
  489. XX                    c = nextchar();
  490. XX                    if (c == '\/')
  491. XX                        recognised = TRUE;
  492. XX                }
  493. XX            }
  494. XX"%{"            {
  495. XX                ECHO;
  496. XX                recognised = FALSE;
  497. XX                c = nextchar();
  498. XX                while (recognised == FALSE)
  499. XX                {
  500. XX                    while (c != '\%')
  501. XX                        c = nextchar();
  502. XX                    c = nextchar();
  503. XX                    if (c == '\}')
  504. XX                        recognised = TRUE;
  505. XX                }
  506. XX                return(PERCURL);
  507. XX            }
  508. XX"{"            {
  509.  
  510. XX/*
  511. XX*    Although LEX can cope with the full definition,
  512. XX*    ( "{"[^\}]*"}" ) this may overrun the lex buffer (200 chars).
  513. XX*    Thus this routine.
  514. XX*/
  515.  
  516. XX                ECHO;
  517. XX                c = nextchar();
  518. XX                while (c != '\}')
  519. XX                    c = nextchar();
  520. XX                return(ACT);
  521. XX            }
  522. XX{letter}{id_char}*    {
  523. XX                ECHO;
  524. XX                return(IDENTIFIER);
  525. XX            }
  526. XX"'"\\?[^']+"'"        {
  527. XX                ECHO;
  528. XX                return(CHARACTER);
  529. XX            }
  530. XX{white}+        {    
  531. XX                ECHO;
  532. XX            }
  533. XX{digit}+        {
  534. XX                ECHO;
  535. XX                return(NUMBER);
  536. XX            }
  537. XX"%"{white}*"left"    {
  538. XX                ECHO;
  539. XX                return(LEFT);
  540. XX            }
  541. XX"%"{white}*"right"    {
  542. XX                ECHO;
  543. XX                return(RIGHT);
  544. XX            }
  545. XX"%"{white}*"nonassoc"    {
  546. XX                ECHO;
  547. XX                return(NONASSOC);
  548. XX            }
  549. XX"%"{white}*"token"    {
  550. XX                ECHO;
  551. XX                return(TOKEN);
  552. XX            }
  553. XX"%"{white}*"prec"    {
  554. XX                ECHO;
  555. XX                return(PREC);
  556. XX            }
  557. XX"%"{white}*"type"    {
  558. XX                ECHO;
  559. XX                return(TYPE);
  560. XX            }
  561. XX"%"{white}*"start"    {
  562. XX                ECHO;
  563. XX                return(START);
  564. XX            }
  565. XX"%"{white}*"union"    {
  566. XX                ECHO;
  567. XX                return(UNION);
  568. XX            }
  569. XX"%%"            {
  570. XX                ECHO;
  571. XX                return(PER);
  572. XX            }
  573. XX":"            {
  574. XX                ECHO;
  575. XX                return(COLON);
  576. XX            }
  577. XX";"            {
  578. XX                ECHO;
  579. XX                return(SEMICOLON);
  580. XX            }
  581. XX","            {
  582. XX                ECHO;
  583. XX                return(COMMA);
  584. XX            }
  585. XX"|"            {
  586. XX                ECHO;
  587. XX                return(OR);
  588. XX            }
  589. XX"<"            {
  590. XX                ECHO;
  591. XX                return(LESS);
  592. XX            }
  593. XX">"            {
  594. XX                ECHO;
  595. XX                return(GREATER);
  596. XX            }
  597. XX"\n"            {
  598. XX                ECHO;
  599. XX                nline(++line);
  600. XX            }
  601.  
  602. XX%%
  603.  
  604. XXyywrap()
  605. XX{
  606. XX    /* wrap-up procedure */
  607. XX    return(1);
  608. XX}
  609.  
  610. XXnextchar()
  611. XX{
  612. XX    char    c;
  613. XX    
  614. XX    c = input();
  615. XX    printf("%c",c);
  616. XX    if (c == '\n')
  617. XX        nline(++line);
  618. XX    return(c);
  619. XX}
  620. @//E*O*F yyref-lex//
  621. chmod u=rw,g=rw,o=rw yyref-lex
  622.  
  623. echo x - yyref-line.h
  624. sed 's/^XX//' > "yyref-line.h" <<'@//E*O*F yyref-line.h//'
  625.  
  626. XX    int    line;
  627. @//E*O*F yyref-line.h//
  628. chmod u=rw,g=rw,o=rw yyref-line.h
  629.  
  630. echo Inspecting for damage in transit...
  631. temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
  632. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  633. cat > $temp <<\!!!
  634.        9      21     157 Makefile
  635.       10      87     601 README
  636.       91     330    1806 yyref.1
  637.      286     591    4433 yyref-grammer
  638.      173     293    2323 yyref-lex
  639.        2       2      12 yyref-line.h
  640.      571    1324    9332 total
  641. !!!
  642. wc  Makefile README yyref.1 yyref-grammer yyref-lex yyref-line.h | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  643. if test -s $dtemp
  644. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  645. else echo "No problems found."
  646. fi
  647. exit 0
  648.